"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 系统内部使用工具
Case Name   : 普通用户执行gs_upgradechk -?，-h
Create At   : 2023/08/02
Owner       : opentestcase042
Description :
    1.普通用户执行gs_upgradechk -?，-h
Expect      :
    1.执行成功,显示参数信息
History     :
"""

import unittest
import os
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro

LOG = Logger()


class ToolsTestCase(unittest.TestCase):
    def setUp(self):
        self.commonsh = CommonSH('PrimaryDbUser')
        self.user_node = Node('PrimaryDbUser')
        LOG.info("---SetUp：检查数据库状态是否正常---")
        status = self.commonsh.get_db_cluster_status()
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_system_internal_tools(self):
        LOG.info(f"----{os.path.basename(__file__)} start----")
        LOG.info("---step1：执行如下语句  expect:显示参数信息---")
        cmd_list = ["gs_upgradechk -?", "gs_upgradechk -h"]
        for cmd in cmd_list:
            result = self.user_node.sh(f"source {macro.DB_ENV_PATH};"
                                       f"{cmd}").result()
            LOG.info(result)
            LOG.info("语法核对")
            con_list1 = ["check | verify      校验数据库元数据",
                        "export              导出一份元数据地图",
                        "help | -h | -?      打印帮助"]
            for con in con_list1:
                self.assertIn(con, result)
            LOG.info("参数核对")
            con_list2 = ["-p", "-F", "-M", "-v", "-d"]
            for con in con_list2:
                self.assertIn(con, result)

    def tearDown(self):
        LOG.info('--------无需清理环境--------')
        status = self.commonsh.get_db_cluster_status("detail")
        LOG.info(status)
        self.assertTrue("Degraded" in status or "Normal" in status)
        LOG.info(f"------{os.path.basename(__file__)} end------")
